package top.kaoshanji.p1ch05a;

import org.apache.hc.core5.http.ContentType;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;

/**
 * 代码清单-p1ch05a-146页a：使用缓冲区支持持续性连接的Servlet
 * URL访问地址：/keepAlive
 * @author kaoshanji
 * ^_^
 * create time 2025/4/4 11:19
 */
public class KeepAlive extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 设置请求对象的编码
        req.setCharacterEncoding(StandardCharsets.UTF_8.name());

        // 设置响应对象的编码和内容格式
        resp.setContentType(ContentType.TEXT_HTML.toString());

        // 请求一个 16k 字节响应缓冲区，不设置内容长度
        resp.setBufferSize(16 * 1024);

        PrintWriter out = resp.getWriter();
        out.println("<HTML>");
        out.println("<HEAD><TITLE>Hello World</TITLE></HEAD>");
        out.println("<BODY>");
        out.println("<BIG>Less than 16K of response body</BIG>");
        out.println("</BODY></HTML>");

    }

}
